package com.projects.candientu.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.Collator;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;

import org.apache.http.HttpEntity;

import android.content.Context;
import android.content.Intent;

import com.projects.candientu.TestActivity;

public class Utils {
	public final static String SERVICE_URI = "http://nhipsongweb.com/CanDienTu_Service.svc";
	public final static DateFormat DATE_FORMATTER = new SimpleDateFormat(
			"dd/MM/yyyy HH:mm:ss");

	public static Intent browseIntent(Context ctx, String path) {
		Intent result = new Intent();

		if (path == "TestActivity") {
			result.setClass(ctx, TestActivity.class);
		}
		return result;
	}

	public final static Comparator<RowData> sDisplayNameComparator = new Comparator<RowData>() {
		private final Collator collator = Collator.getInstance();
		public int compare(RowData map1, RowData map2) {
			Date thoidiem1 = null;
			Date thoidiem2 = null;
			try {
				thoidiem1 = (Date)DATE_FORMATTER.parse(String.valueOf(map1.getmThoiDiemDo()));
				thoidiem2 = (Date)DATE_FORMATTER.parse(String.valueOf(map2.getmThoiDiemDo()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return collator.compare(thoidiem1, thoidiem2);
		}
	};

	public static String getResponse(HttpEntity entity) {
		String response = "";

		try {
			char[] buffer = new char[(int)entity.getContentLength()];
	        InputStream stream = entity.getContent();
	        InputStreamReader reader = new InputStreamReader(stream);
	        reader.read(buffer);
	        stream.close();
	        response = new String(buffer);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}

		return response;
	}

}
